package com.xmtlxx.common.utils.push;

import com.xmtlxx.common.config.XmtlxxConfig;
import com.xmtlxx.common.utils.DateUtils;
import com.xmtlxx.common.utils.JsonUtils;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.push.domain.PushContent;
import com.xmtlxx.common.wxap.util.HttpInvokerUtils;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.AudienceDTO;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
import com.getui.push.v2.sdk.dto.req.message.android.Ups;
import com.getui.push.v2.sdk.dto.res.TaskIdDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PushUtils {

    //单推
    public static void pushMsgV1(String cid, PushContent pushContent){
        ApiHelper apiHelper = PushHttpUtils.RestApi();
        PushApi pushApi = apiHelper.creatApi(PushApi.class);

        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setNotification(notification);
        notification.setTitle(pushContent.getTitle());
        notification.setBody(pushContent.getBody());
        if(StringUtils.isNotEmpty(pushContent.getPayload())) {
            notification.setPayload(pushContent.getPayload());
        }
        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            notification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            notification.setUrl(pushContent.getUrl());
        }
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/

        /**** 设置厂商相关参数 ****/
        PushChannel pushChannel = new PushChannel();
        pushDTO.setPushChannel(pushChannel);
        /*配置安卓厂商参数*/
        AndroidDTO androidDTO = new AndroidDTO();
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification thirdNotification = new ThirdNotification();
        ups.setNotification(thirdNotification);
        thirdNotification.setTitle(pushContent.getTitle());
        thirdNotification.setBody(pushContent.getBody());
        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            thirdNotification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            thirdNotification.setUrl(pushContent.getUrl());
        }
        if(StringUtils.isNotEmpty(pushContent.getPayload())) {
            thirdNotification.setPayload(pushContent.getPayload());
        }
        // 两条消息的notify_id相同，新的消息会覆盖老的消息，取值范围：0-2147483647
        // thirdNotification.setNotifyId("11177");
        /*配置安卓厂商参数结束，更多参数请查看文档或对象源码*/
        /*设置ios厂商参数*/
//        IosDTO iosDTO = new IosDTO();
//        pushChannel.setIos(iosDTO);
//        // 相同的collapseId会覆盖之前的消息
//        iosDTO.setApnsCollapseId("xxx");
//        Aps aps = new Aps();
//        iosDTO.setAps(aps);
//        Alert alert = new Alert();
//        aps.setAlert(alert);
//        alert.setTitle("ios title");
//        alert.setBody("ios body");
        /*设置ios厂商参数结束，更多参数请查看文档或对象源码*/

        // 设置接收人信息
        Audience audience = new Audience();
        audience.addCid(cid);
        pushDTO.setAudience(audience);
        pushDTO.setRequestId(DateUtils.getCurrentDateTime());

        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }

    public static void pushMsg(String cid,PushContent pushContent){
        try {
            Map<String,Object> data = new HashMap<>();
            data.put("api","uniPush");
            Map<String,Object> content = new HashMap<>();
            content.put("cid",cid);
            content.put("pushContent",pushContent);
            data.put("data",content);
            String result = HttpInvokerUtils.PostToJson(XmtlxxConfig.getProtocol()+"://"+ XmtlxxConfig.getAliyunSmsEndpoint(), JsonUtils.toJsonString(data));
            System.out.println("pushMsg===>"+result);
        }catch (Exception e){
            e.printStackTrace();;
        }

    }

    //多推
    public static void pushListMsg(List<String> cids,PushContent pushContent){
        ApiHelper apiHelper = PushHttpUtils.RestApi();
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setNotification(notification);
        notification.setTitle(pushContent.getTitle());
        notification.setBody(pushContent.getBody());
        if(StringUtils.isNotEmpty(pushContent.getPayload())) {
            notification.setPayload(pushContent.getPayload());
        }
        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            notification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            notification.setUrl(pushContent.getUrl());
        }
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/

        /**** 设置厂商相关参数 ****/
        PushChannel pushChannel = new PushChannel();
        pushDTO.setPushChannel(pushChannel);
        /*配置安卓厂商参数*/
        AndroidDTO androidDTO = new AndroidDTO();
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification thirdNotification = new ThirdNotification();
        ups.setNotification(thirdNotification);
        thirdNotification.setTitle(pushContent.getTitle());
        thirdNotification.setBody(pushContent.getBody());

        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            thirdNotification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            thirdNotification.setUrl(pushContent.getUrl());
        }

        // 设置接收人信息
        Audience audience = new Audience();
        AudienceDTO audienceDTO = new AudienceDTO();
        audience.setCid(cids);
        pushDTO.setAudience(audience);
        audienceDTO.setAudience(audience);
        ApiResult<TaskIdDTO> createApiResult = pushApi.createMsg(pushDTO);
        audienceDTO.setTaskid(createApiResult.getData().getTaskId());
        audienceDTO.setAsync(true);

        // 进行cid多推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushListByCid(audienceDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }

    public static ApiResult<Map<String, Map<String, String>>> pushTest(String cid){
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置，参数在“Uni Push”下的“应用配置”页面中获取
        PushContent pushContent = new PushContent();
        pushContent.setTitle("接收财物提醒");
        pushContent.setContent("您有1件 荣耀手机待接收登记，请及时处理!");
        pushContent.setPayload("\"{\"page\":\"/pagesOMS/staging/index\"}\"");
        pushContent.setClickType("payload");
        pushContent.setUrl("");
        pushContent.setBody("您有1件 荣耀手机待接收登记，请及时处理!");
        apiConfiguration.setAppId("glGChUVtqfASR5rnyQvpS4");
        apiConfiguration.setAppKey("AxrQ0EaXeX90PW859A6Vz4");
        apiConfiguration.setMasterSecret("hAFCJ3DhKB84vRfmGlOUG9");
        apiConfiguration.setDomain("http://10.210.1.34:10019/v2/");
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setNotification(notification);
        notification.setTitle(pushContent.getTitle());
        notification.setBody(pushContent.getBody());
        if(StringUtils.isNotEmpty(pushContent.getPayload())) {
            notification.setPayload(pushContent.getPayload());
        }
        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            notification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            notification.setUrl(pushContent.getUrl());
        }
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/

        /**** 设置厂商相关参数 ****/
        PushChannel pushChannel = new PushChannel();
        pushDTO.setPushChannel(pushChannel);
        /*配置安卓厂商参数*/
        AndroidDTO androidDTO = new AndroidDTO();
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification thirdNotification = new ThirdNotification();
        ups.setNotification(thirdNotification);
        thirdNotification.setTitle(pushContent.getTitle());
        thirdNotification.setBody(pushContent.getBody());
        if(StringUtils.isNotEmpty(pushContent.getPayload())) {
            thirdNotification.setPayload(pushContent.getPayload());
        }
        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            thirdNotification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            thirdNotification.setUrl(pushContent.getUrl());
        }

        // 设置接收人信息
        Audience audience = new Audience();
        AudienceDTO audienceDTO = new AudienceDTO();
        audience.addCid(cid);
        pushDTO.setAudience(audience);
        audienceDTO.setAudience(audience);
        ApiResult<TaskIdDTO> createApiResult = pushApi.createMsg(pushDTO);
        audienceDTO.setTaskid(createApiResult.getData().getTaskId());
        audienceDTO.setAsync(true);
        pushDTO.setRequestId(DateUtils.getCurrentDateTime());

        /*设置接收人信息结束*/
        /**** 设置厂商相关参数，更多参数请查看文档或对象源码 ****/

        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
        return apiResult;
    }

    public static void main(String[] args) {
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置，参数在“Uni Push”下的“应用配置”页面中获取
        PushContent pushContent = new PushContent();
        pushContent.setTitle("接收财物提醒");
        pushContent.setContent("您有1件 荣耀手机待接收登记，请及时处理!");
        pushContent.setPayload("\"{\"page\":\"/pagesOMS/staging/index\"}\"");
        pushContent.setClickType("payload");
        pushContent.setUrl("");
        pushContent.setBody("您有1件 荣耀手机待接收登记，请及时处理!");
        apiConfiguration.setAppId("glGChUVtqfASR5rnyQvpS4");
        apiConfiguration.setAppKey("AxrQ0EaXeX90PW859A6Vz4");
        apiConfiguration.setMasterSecret("hAFCJ3DhKB84vRfmGlOUG9");
        apiConfiguration.setDomain("https://restapi.getui.com/v2/");
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        PushApi pushApi = apiHelper.creatApi(PushApi.class);
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        /**** 设置个推通道参数 *****/
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setNotification(notification);
        notification.setTitle(pushContent.getTitle());
        notification.setBody(pushContent.getBody());
        if(StringUtils.isNotEmpty(pushContent.getPayload())) {
            notification.setPayload(pushContent.getPayload());
        }
        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            notification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            notification.setUrl(pushContent.getUrl());
        }
        /**** 设置个推通道参数，更多参数请查看文档或对象源码 *****/

        /**** 设置厂商相关参数 ****/
        PushChannel pushChannel = new PushChannel();
        pushDTO.setPushChannel(pushChannel);
        /*配置安卓厂商参数*/
        AndroidDTO androidDTO = new AndroidDTO();
        pushChannel.setAndroid(androidDTO);
        Ups ups = new Ups();
        androidDTO.setUps(ups);
        ThirdNotification thirdNotification = new ThirdNotification();
        ups.setNotification(thirdNotification);
        thirdNotification.setTitle(pushContent.getTitle());
        thirdNotification.setBody(pushContent.getBody());
        if(StringUtils.isNotEmpty(pushContent.getPayload())) {
            thirdNotification.setPayload(pushContent.getPayload());
        }
        if(StringUtils.isNotEmpty(pushContent.getClickType())){
            thirdNotification.setClickType(pushContent.getClickType());
        }
        if(StringUtils.isNotEmpty(pushContent.getUrl())){
            thirdNotification.setUrl(pushContent.getUrl());
        }

        // 设置接收人信息
        Audience audience = new Audience();
        AudienceDTO audienceDTO = new AudienceDTO();
        audience.addCid("69b6599c398ee09b21639528cd6d9173");
        pushDTO.setAudience(audience);
        audienceDTO.setAudience(audience);
        ApiResult<TaskIdDTO> createApiResult = pushApi.createMsg(pushDTO);
        audienceDTO.setTaskid(createApiResult.getData().getTaskId());
        audienceDTO.setAsync(true);
        pushDTO.setRequestId(DateUtils.getCurrentDateTime());

        /*设置接收人信息结束*/
        /**** 设置厂商相关参数，更多参数请查看文档或对象源码 ****/

        // 进行cid单推
        ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
        if (apiResult.isSuccess()) {
            // success
            System.out.println(apiResult.getData());
        } else {
            // failed
            System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
        }
    }
}
